package Thread;

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * @className: EasyThreadPool
 * @author: 芃
 * @description: 简易版线程池
 * @date: 2024/1/15 0:37
 */
public class EasyThreadPool {
    private BlockingDeque<Runnable> queue = new LinkedBlockingDeque<>();

    public void submit(Runnable runnable){
        try {
            queue.put(runnable);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public EasyThreadPool(int n) {
        for (int i = 0; i < n; i++) {
            Thread thread = new Thread(() ->{
                while (true){
                    try {
                        Runnable runnable = queue.take();
                        runnable.run();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
            thread.start();
        }
    }
}
